package com.example.wechat.login.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wechat.login.dto.ApiResponse;
import com.example.wechat.login.dto.feedback.FeedbackRequest;
import com.example.wechat.login.dto.feedback.FeedbackResponse;
import com.example.wechat.login.service.FeedbackService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;


/**
 * 问题反馈控制器
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/feedback")
public class FeedbackController {

    private final FeedbackService feedbackService;

    /**
     * 创建问题反馈
     *
     * @param request 反馈请求DTO
     * @return 反馈响应DTO
     */
    @PostMapping
    public ApiResponse<FeedbackResponse> createFeedback(@Valid @RequestBody FeedbackRequest request) {
        // 获取当前登录用户的unionId
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String unionId = authentication.getName();

        FeedbackResponse response = feedbackService.createFeedback(request, unionId);
        return ApiResponse.success(response);
    }

    /**
     * 根据ID查询反馈
     *
     * @param id 反馈ID
     * @return 反馈响应DTO
     */
    @GetMapping("/{id}")
    public ApiResponse<FeedbackResponse> getFeedbackById(@PathVariable @NotNull(message = "反馈ID不能为空") Long id) {
        FeedbackResponse response = feedbackService.getFeedbackById(id);
        return ApiResponse.success(response);
    }

    /**
     * 分页查询当前用户的反馈列表
     *
     * @param current 当前页，默认1
     * @param size 每页大小，默认10
     * @return 分页结果
     */
    @GetMapping("/my")
    public ApiResponse<Page<FeedbackResponse>> getMyFeedbackPage(
            @RequestParam(defaultValue = "1") @Min(value = 1, message = "当前页不能小于1") long current,
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "每页大小不能小于1") long size) {

        // 获取当前登录用户的unionId
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String unionId = authentication.getName();

        Page<FeedbackResponse> page = feedbackService.getFeedbackPageByUnionId(unionId, current, size);
        return ApiResponse.success(page);
    }

    /**
     * 根据反馈类型分页查询反馈列表（管理员接口）
     *
     * @param feedbackType 反馈类型
     * @param current 当前页，默认1
     * @param size 每页大小，默认10
     * @return 分页结果
     */
    @GetMapping("/type/{feedbackType}")
    public ApiResponse<Page<FeedbackResponse>> getFeedbackPageByFeedbackType(
            @PathVariable @NotNull(message = "反馈类型不能为空") String feedbackType,
            @RequestParam(defaultValue = "1") @Min(value = 1, message = "当前页不能小于1") long current,
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "每页大小不能小于1") long size) {

        Page<FeedbackResponse> page = feedbackService.getFeedbackPageByFeedbackType(feedbackType, current, size);
        return ApiResponse.success(page);
    }

    /**
     * 根据状态分页查询反馈列表（管理员接口）
     *
     * @param status 状态
     * @param current 当前页，默认1
     * @param size 每页大小，默认10
     * @return 分页结果
     */
    @GetMapping("/status/{status}")
    public ApiResponse<Page<FeedbackResponse>> getFeedbackPageByStatus(
            @PathVariable @NotNull(message = "状态不能为空") Integer status,
            @RequestParam(defaultValue = "1") @Min(value = 1, message = "当前页不能小于1") long current,
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "每页大小不能小于1") long size) {

        Page<FeedbackResponse> page = feedbackService.getFeedbackPageByStatus(status, current, size);
        return ApiResponse.success(page);
    }

    /**
     * 更新反馈状态（管理员接口）
     *
     * @param id 反馈ID
     * @param status 状态
     * @param result 处理结果
     * @return 是否更新成功
     */
    @PutMapping("/{id}/status")
    public ApiResponse<Boolean> updateFeedbackStatus(
            @PathVariable @NotNull(message = "反馈ID不能为空") Long id,
            @RequestParam @NotNull(message = "状态不能为空") Integer status,
            @RequestParam(required = false) String result) {

        boolean success = feedbackService.updateFeedbackStatus(id, status, result);
        return ApiResponse.success(success);
    }
}
